<template>
  <div class="login">
    <div class="login_box">
      <el-form
        :label-position="labelPosition"
        :rules="rules"
        :model="formLabelAlign"
        ref="logonFromRef"
      >
        <el-form-item prop="username">
          <el-input
            v-model="formLabelAlign.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formLabelAlign.password"
            prefix-icon="iconfont icon-lock_fill"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="btn">
        <el-button type="success" @click="login">登录</el-button>
        <el-button type="info" @click="reset">重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        labelPosition: 'right',
        formLabelAlign: {
          username: 'admin',
          password: '123456'
        },
        rules: {
          username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      login() {
        this.$refs.logonFromRef.validate(async valid => {
          if (!valid) return
          const { data } = await this.$http.post('login', this.formLabelAlign)
          if (data.meta.status !== 200) return this.$message.error(data.meta.msg)
          console.log('object :>> ', data.data.token)
          window.sessionStorage.setItem('token', data.data.token)
          this.$message.error(data.meta.msg)
          this.$router.push('home')
        })
      },
      reset() {
        this.$refs.logonFromRef.resetFields()
      }
    }
  }
</script>
<style lang="less" scoped>
  .login {
    height: 100%;
    .login_box {
      background-color: #2b4b6b;
      width: 500px;
      height: 300px;
      border-radius: 5px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      padding: 50px 10px 10px 10px;
      box-sizing: border-box;
      .btn {
        display: flex;
        justify-content: space-between;
      }
    }
  }
</style>

